export let deepClone = (obj) => {
  function isObject (obj) {
    return typeof obj === 'object' && typeof obj !== 'null'
  }
  if (!isObject(obj)) {
    throw new Error('输入参数不是对象')
  }
  let res = Array.isArray(obj) ? [...obj] : { ...obj }
  Object.keys(res).forEach((key) => {
    res[key] = isObject(res[key]) ? deepClone(res[key]) : res[key]
  })
  return res
}
export let isObject = (params) => {
  return Object.prototype.toString.call(params) === '[object Object]'
}

// 使用icon-是避免在对比出现误差
let video = ['icon-video', '.wmv', '.mpg', '.mpe', '.mpeg', '.3gp', '.mov', '.rm', '.ram', '.swf', '.flv', '.mp4', '.m4v', '.avi', '.asf', '.asx', '.rmvb', '.dat', '.mkv', '.vob']
let audio = ['icon-music', '.mp3', '.cd', '.wave', '.mpeg-4', 'm4a', 'ogg', '.aiff', '.wma', '.midi', '.amr', '.flac', '.ape', '.oggvorbis', '.realaudio', '.vqf', '.aac', '.wav' ]
let jpg = ['icon-images', '.jpeg', '.bmp', '.jpg', '.gif', '.webp', '.pcx', '.tif', '.tga', '.exif', '.fpx', '.svg', '.cdr', '.pcd', '.dxf', '.ufo', '.eps', '.ai', '.png', '.hdri', '.raw', '.wmf', '.flic', '.emf', '.ico', '.avif', '.apng']
let txt = ['icon-txt', '.txt', '.chm']
let rar = ['icon-yasuobao', '.rar', '.zip', '.exe', '.7z', '.ar', '.bz', '.gz', '.jgz', 'jar', '.kz', '.mzp', '.nz', '.rz', '.trz', '.tbz', '.tgz', '.zz']
let word = ['icon-word', '.docx', '.doc', '.rtf']
let excel = ['icon-excel', '.xls', '.xlsx', '.xlsb', '.xlsm', '.xlst']
let pdf = ['icon-pdf', '.pdf']
export let fileArray = [video, audio, jpg, txt, rar, word, excel, pdf]

export let conversion = (val, typeStatus = false, status = false) => {
  let unitArr = typeStatus ? ['px', 'rpx', 'rem', 'vh', 'vw', 'em', 'ch', 'ch', 'ex'] : ['px', 'rpx', 'rem', '%', 'vh', 'vw', 'em', 'ch', 'ch', 'ex']
  val = val.toString()
  if (status) {
    let status = false
    unitArr.forEach(item => {
      if (val.includes(item)) {
        status = true
      }
    })
    let value = parseInt(val)
    return status ? `${value + 3}${val.replace(/\d+/g, '')}` : `${value + 3}px`
  } else {
    let status = false
    unitArr.forEach(item => {
      if (val.includes(item)) {
        status = true
      }
    })
    return status ? val : `${val}px`
  }
}

export let domainNameList = [
  '.com', '.xyz', '.net', '.top', '.tech', '.org', '.gov', '.edu', '.ink', '.int', '.mil', '.pub', '.cn', '.com.cn', '.net.cn', '.gov.cn', '.org.cn', '.中国', '.公司', '.网络', '.red', '.biz', '.cn', '.tv', '.info', '.name', '.mobi', '.travel', '.cc', '.tv', '.pro', '.museum', '.coop', '.aero'
]

export let checkFileType = (url, type = false) => {
  let urlStr = url.toLowerCase()
  let iconName = 'icon-unknownfile'
  for (let i = 0; i < fileArray.length; i++) {
    for (let k = 0; k < fileArray[i].length; k++) {
      if (urlStr.includes(fileArray[i][k])) {
        iconName = type ? fileArray[i][k].substr(1).toUpperCase() : fileArray[i][0]
      }
    }
  }
  let urlSplitArr = url.split('.')
  return type ? urlSplitArr[urlSplitArr.length - 1].toUpperCase() : iconName
}
export let getUrlFileName = (url) => {
  if (!url) return ''
  if (url.includes('data:')) return '未知文件名'
  let arr = url.split('/')
  let lastFiltration = []
  arr.forEach(item => {
    if (item.includes('.')) {
      lastFiltration.push(item)
    }
  })
  let status = domainNameList.some(item => lastFiltration[lastFiltration.length - 1].includes(item.toLowerCase()))
  let returnString = lastFiltration[lastFiltration.length - 1] && !status ? lastFiltration[lastFiltration.length - 1] : '未知文件名'
  return returnString
}

export let numerical = (val) => {
  let number = parseInt(val)
  return number
}

// base64转blob
export let base64toBlob = (dataurl) => {
  var arr = dataurl.split(','); var mime = arr[0].match(/:(.*?);/)[1];
      var bstr = atob(arr[1]); var n = bstr.length; var u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  return new Blob([u8arr], { type: mime })
}

// base64转File
export let base64toFile = (dataurl, filename) => { // 将base64转换为文件
  var arr = dataurl.split(','); var mime = arr[0].match(/:(.*?);/)[1];
      var bstr = atob(arr[1]); var n = bstr.length; var u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  return new File([u8arr], filename, { type: mime })
}

// 将图片转换为Base64
export let imageToBase64 = (url) => {
  return new Promise((resolve, reject) => {
    let canvas = document.createElement('canvas');
    let ctx = canvas.getContext('2d');
    let img = new Image
    img.crossOrigin = 'Anonymous'
    img.src = url
    img.onload = function () {
      canvas.height = img.height
      canvas.width = img.width
      ctx.drawImage(img, 0, 0)
      let dataURL = canvas.toDataURL('image/png')
      resolve({
        src: dataURL,
        name: getUrlFileName(url)
      })
      canvas = null
    };
  })
}

// blob转base64
export let blobToBase64 = (blob, name) => {
  return new Promise((resolve, reject) => {
  let a = new FileReader()
  a.onload = function (e) {
    let data = null
    if (name) {
      data = {
        src: e.target.result,
        name
      }
    } else {
      data = e.target.result
    }
    resolve(data)
  }
  a.readAsDataURL(blob)
  })
}

export let newGuid = () => {
	var guid = "";
	for (var i = 1; i <= 64; i++){
		var n = Math.floor(Math.random()*32.0).toString(32);
		guid += n;
		if(i % 8 == 0)
			guid += "-";
	}
  return guid.substring(0, guid.length-1);
}

// 驼峰转中划线
export let toLine = (text) => {
  return text.replace(/([A-Z])/g,"-$1").toLowerCase();
}

// 中划线转驼峰
export let toHump = (text) => {
  return text.replace(/\-(\w)/g, function(all, letter){
      return letter.toUpperCase();
  });
}